//
//  YCMoneyTextField.m
//  DemoCustomTxtFld
//
//  Created by tangkexiong on 16/1/7.
//  Copyright © 2016年 tangkexiong. All rights reserved.
//

#import "YCMoneyTextField.h"

@implementation YCMoneyTextField

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self _configView];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self _configView];
}

- (void)_configView {
    self.keyboardType = UIKeyboardTypeDecimalPad;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_textDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)dealloc {
    self.keyboardType = UIKeyboardTypeDecimalPad;
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
    [self setText:self.text];
}

- (void)setText:(NSString *)text {
    [super setText:[self _convertToDecimal:text]];
}

#pragma mark - NSNotification

- (void)_textDidChange:(NSNotification *)notifiction {
    [self setText:self.text];
}

- (NSString *)_convertToDecimal:(NSString *)str {
    NSRange rangeDot = [str rangeOfString:@"."];
    if (rangeDot.length == 0) {// 不包含小数点
        NSString *strTemp = [[str componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]] componentsJoinedByString:@""];
        if ([strTemp isEqualToString:@"0"]) {
            return strTemp;
        } else {
            NSInteger count = strTemp.length;
            NSString *result = @"";
            for (int i = 0; i < count; i++) {
                if (![[strTemp substringWithRange:NSMakeRange(i, 1)] isEqualToString:@"0"]) {
                    result = [strTemp substringFromIndex:i];
                    break;
                }
            }
            return result;
        }
    } else {// 包含小数点
        NSString *str1 = [str substringToIndex:rangeDot.location];
        NSString *str2 = [str substringFromIndex:rangeDot.location+1];
        NSString *str1Temp = [[str1 componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]] componentsJoinedByString:@""];
        NSString *str2Temp = [[str2 componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet]] componentsJoinedByString:@""];
        NSMutableString *mutableStr = [[NSMutableString alloc] init];
        if (str1Temp.length == 0) {
            [mutableStr appendString:@"0."];
        } else {
            [mutableStr appendFormat:@"%@.", str1Temp];
            
            if (str2Temp.length < 3) {
                [mutableStr appendString:str2Temp];
            } else {
                [mutableStr appendString:[str2Temp substringToIndex:2]];
            }
        }
        
        return mutableStr;
    }
}

@end